package com.zheng.study.base.bean.search.projection;

import com.zheng.study.base.bean.search.SearchEnum;
import com.zheng.study.base.utils.CommonUtil;
import org.springframework.util.Assert;

/**
 * 查询 - 参数 - ProjectionCount
 * ============================================================================
 * author : dzw
 * createDate:  2018/6/11 。
 * ============================================================================
 */
public class ProjectionCount extends Projection {

	private final String field;

	protected ProjectionCount(SearchEnum.ProjectionType projectionType, String field) {
		super(projectionType);
		this.field = field;
	}

	public String getField() {
		return field;
	}

	public static ProjectionCount count(String field){
		return newBuilder().setProjectionType(SearchEnum.ProjectionType.count).setField(field).build();
	}

	public static ProjectionCount countDistinct(String field){
		return newBuilder().setProjectionType(SearchEnum.ProjectionType.countDistinct).setField(field).build();
	}

	public static Builder newBuilder(){
		return new Builder();
	}

	public static class Builder extends Projection.Builder<Projection, Builder>{

		private String field;

		public Builder setField(String field) {
			this.field = field;
			return this;
		}

		@Override
		protected Builder getThis() {
			return this;
		}

		@Override
		public ProjectionCount build() {
			Assert.notNull(projectionType, "projectionType 不能为空");
			Assert.hasText(field, "field 不能为空");
			Assert.isTrue(CommonUtil.isField(field), "field 非法参数");
			return new ProjectionCount(projectionType, field);
		}
	}

}
